package com.sbp.message.api.entity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.codec.Charsets;

import java.util.Date;

/**
 * Created by wangmin on 2018/11/23.
 */
public class MessageVO implements java.io.Serializable {
    public String id;
    public String destination;
    public String source;//队列对应源系统标识
    public String content;//内容
    public String groupName;//通知组名
    public int retry;//重试次数
    public int messageType;
    public int status;
    public int isPersistence;// 0:透传非持久化; 1:持久化
    public int acknowledges;//当前成功确认数量
    public int businessFailed;  //只能人工处理的业务异常的数量
    public String acknowledgesDescribe;//确认者信息组成
    public int consumers ;//该条消息的消费者数量，适应于一对多的订阅
    public String consumersDescribe;//消费者信息描述
    public String unexpectedConsumersDescribe; //意外消费的消费者名称
    public Date createTime;
    public Date updateTime;
    public String traceId;
    public String errorMessage;
    public String errorLog;//错误日志



    public static MessageVO fromMessage(Message message) {
        MessageVO vo = new MessageVO();

        vo.id = message.id;
        vo.destination = message.destination;
        vo.source = message.source;
        vo.content = JSON.toJSONString(JSON.parse(message.content), SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
        vo.groupName = message.groupName;
        vo.retry = message.retry;
        vo.messageType = message.messageType;
        vo.status = message.status;
        vo.isPersistence = message.isPersistence;
        vo.acknowledges = message.acknowledges;
        vo.businessFailed = message.businessFailed;
        vo.acknowledgesDescribe = message.acknowledgesDescribe;
        vo.consumers = message.consumers;
        vo.consumersDescribe = message.consumersDescribe;
        vo.unexpectedConsumersDescribe = message.unexpectedConsumersDescribe;
        vo.createTime = message.createTime;
        vo.updateTime = message.updateTime;
        vo.traceId = message.traceId;
        vo.errorMessage = message.errorMessage;
        if (message.errorLog != null)
            vo.errorLog = new String(message.errorLog, Charsets.UTF_8);

        return vo;
    }
}
